package net.lab1024.sa.admin.module.business.category.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import net.lab1024.sa.admin.module.business.category.constant.CategoryTypeEnum;
import net.lab1024.sa.admin.module.business.category.dao.FrontCategoryDao;
import net.lab1024.sa.admin.module.business.category.domain.entity.FrontCategoryEntity;
import net.lab1024.sa.admin.module.business.category.domain.form.*;
import net.lab1024.sa.admin.module.business.category.domain.vo.CategoryTreeVO;
import net.lab1024.sa.admin.module.business.category.domain.vo.FrontCategoryMapVO;
import net.lab1024.sa.admin.module.business.category.domain.vo.FrontCategoryVO;
import net.lab1024.sa.admin.module.business.category.manager.CategoryCacheManager;
import net.lab1024.sa.common.common.util.SmartBeanUtil;
import net.lab1024.sa.common.common.util.SmartPageUtil;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 前台类目 Service
 *
 * @Author jabari
 * @Date 2023-06-01 15:12:18
 * @Copyright 51dev.cn
 */

@Service
public class FrontCategoryService {

    @Autowired
    private FrontCategoryDao frontCategoryDao;

    @Autowired
    private FrontCategoryMapService frontCategoryMapService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<FrontCategoryVO> queryPage(FrontCategoryQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<FrontCategoryVO> list = frontCategoryDao.queryPage(page, queryForm);
        list.forEach(e->{
            e.setCategoryNames(categoryService.getCategoryFullNames(Arrays.stream(e.getCategoryIds()).map(Long::valueOf).collect(Collectors.toList())).getData());
        });


        PageResult<FrontCategoryVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(FrontCategoryAddForm addForm) {
        FrontCategoryEntity frontCategoryEntity = SmartBeanUtil.copy(addForm, FrontCategoryEntity.class);
        frontCategoryDao.insert(frontCategoryEntity);
        Integer frontCategoryId = frontCategoryEntity.getFrontCategoryId();
        FrontCategoryMapAddForm frontCategoryMapAddForm=new FrontCategoryMapAddForm();
        frontCategoryMapAddForm.setFrontCategoryId(frontCategoryId);
        for(Integer categoryId : addForm.getCategoryIds()){
            frontCategoryMapAddForm.setCategoryId(categoryId);
            frontCategoryMapService.add(frontCategoryMapAddForm);
        }

        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(FrontCategoryUpdateForm updateForm) {
        FrontCategoryEntity frontCategoryEntity = SmartBeanUtil.copy(updateForm, FrontCategoryEntity.class);
        frontCategoryDao.updateById(frontCategoryEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    @Transactional
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        frontCategoryDao.deleteBatchIds(idList);
        for(Integer id:idList){
            deleteFrontCategoryMap(id);
        }
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional
    public ResponseDTO<String> delete(Integer frontCategoryId) {
        if (null == frontCategoryId){
            return ResponseDTO.ok();
        }

        frontCategoryDao.deleteById(frontCategoryId);
        deleteFrontCategoryMap(frontCategoryId);
        return ResponseDTO.ok();
    }

    private void deleteFrontCategoryMap(Integer frontCategoryId){

        FrontCategoryMapQueryForm frontCategoryMapQueryForm=new FrontCategoryMapQueryForm();
        frontCategoryMapQueryForm.setFrontCategoryId(frontCategoryId);
        frontCategoryMapQueryForm.setPageNum(1);
        frontCategoryMapQueryForm.setPageSize(100);
        PageResult<FrontCategoryMapVO> frontCategoryMapVOPageResult = frontCategoryMapService.queryPage(frontCategoryMapQueryForm);
        List<FrontCategoryMapVO> frontCategoryMapVOPageResultList = frontCategoryMapVOPageResult.getList();

        List<Integer> mapIdList = frontCategoryMapVOPageResultList.stream().map(FrontCategoryMapVO::getMapId).collect(Collectors.toList());

        frontCategoryMapService.batchDelete(mapIdList);

    }
}
